package info.ask4research.dao.impl;

import java.util.List;

import info.ask4research.dao.IToolAndMaterialDAO;
import info.ask4research.dao.core.impl.AbstractDaoImpl;
import info.ask4research.model.addons.Addon;
import info.ask4research.model.addons.TextWork;
import info.ask4research.model.toolsAndMaterials.AnnouncementService;
import info.ask4research.model.toolsAndMaterials.ChatService;
import info.ask4research.model.toolsAndMaterials.ForumService;
import info.ask4research.model.toolsAndMaterials.LearningObject;
import info.ask4research.model.toolsAndMaterials.SentMailService;
import info.ask4research.model.toolsAndMaterials.ToolAndMaterial;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

@Repository("toolAndMaterialDAO")
public class ToolAndMaterialDAO extends AbstractDaoImpl<ToolAndMaterial> implements IToolAndMaterialDAO {

	public ToolAndMaterialDAO() {
		super(ToolAndMaterial.class);
	}
	
	private HibernateTemplate hibernateTemplate;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	@Override
	public List<LearningObject> getLearningObjectByUoL(int idUoL) {
		return (List<LearningObject>) hibernateTemplate.find("from LearningObject learningObject where learningObject.unitOfLearning.id=?",  idUoL);
	}

	@Override
	public List<ForumService> getForumServicesByUoL(int idUoL) {
		return (List<ForumService>) hibernateTemplate.find("from ForumService forumService where forumService.unitOfLearning.id=?",  idUoL);
	}

	@Override
	public List<AnnouncementService> getAnnouncementServiceByUoL(int idUoL) {
		return (List<AnnouncementService>) hibernateTemplate.find("from AnnouncementService announcementService where announcementService.unitOfLearning.id=?",  idUoL);
	}

	@Override
	public List<ChatService> getChatServiceByUoL(int idUoL) {
		return (List<ChatService>) hibernateTemplate.find("from ChatService chatService where chatService.unitOfLearning.id=?",  idUoL);
	}

	@Override
	public List<SentMailService> getSentMailServiceByUoL(int idUoL) {
		return (List<SentMailService>) hibernateTemplate.find("from SentMailService sentMailService where sentMailService.unitOfLearning.id=?",  idUoL);
	}

	@Override
	public boolean tamNameExists(String stringTam, int idUoL) {
		List<ToolAndMaterial> tams = (List<ToolAndMaterial>) hibernateTemplate.find("from ToolAndMaterial tam where tam.unitOfLearning.id=? and tam.title=?",  idUoL,stringTam);
		if(tams.isEmpty()){
			return false;
		}else{
			return true;
		}
	}

	@Override
	public ToolAndMaterial getTaMByUoFandTamName(String stringTam, int idUoL) {
		List<ToolAndMaterial> tams = (List<ToolAndMaterial>) hibernateTemplate.find("from ToolAndMaterial tam where tam.unitOfLearning.id=? and tam.title=?",  idUoL,stringTam);
		return tams.get(0);
	}

	@Override
	public List<ToolAndMaterial> getTamByUoL(int id) {
		List<ToolAndMaterial> tams = (List<ToolAndMaterial>) hibernateTemplate.find("from ToolAndMaterial tam where tam.unitOfLearning.id=?",  id);
		return tams;
	}


}
